//
//  FHInputToolbar.m
//  MyWeChat
//
//  Created by 蔡欣东 on 15/10/5.
//  Copyright © 2015年 蔡欣东. All rights reserved.
//

#import "FHInputToolbar.h"
#import "UIImage+UIImage_new1.h"
@implementation FHInputToolbar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    if (self= [super initWithFrame:frame]) {
        self.backgroundView = [[UIView alloc]init];
        self.textFieldBackground = [[UIImageView alloc]init];
        self.textField = [[UITextField alloc]init];
        self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, [UIScreen mainScreen].bounds.size.width, 44);
        self.backgroundView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        self.backgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.backgroundView];
        
        self.leftBtn.frame = CGRectMake(5, 5, 34, 34);
        [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"t7"] forState:UIControlStateNormal];
        [self.leftBtn addTarget:self action:@selector(onLeftBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.leftBtn];
        
        self.textFieldBackground.frame = CGRectMake(34+10, 5, [UIScreen mainScreen].bounds.size.width-34-10-34-10, 34);
        self.textFieldBackground.image = [UIImage resizableImage:@"a8n.9"];
        //self.textFieldBackground.backgroundColor = [UIColor whiteColor];
        [self addSubview: self.textFieldBackground];
        
        self.textField.frame = CGRectMake(self.textFieldBackground.frame.origin.x+5, 5, self.textFieldBackground.frame.size.width-10, 34);
        self.textField.borderStyle = UITextBorderStyleNone;
        self.textField.backgroundColor = [UIColor clearColor];
        self.textField.placeholder = @"请输入...";
        self.textField.delegate = self;
        [self addSubview:self.textField];
        
        self.rightBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-34-5, 5, 34, 34);
        [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"t6"] forState:UIControlStateNormal];
        [self.rightBtn addTarget:self action:@selector(onRightBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.rightBtn];
        
        
    }
    return self;
}
-(void)onRightBtnTapped:(UIButton*) sender{
    [self.delegate onInputBtnTapped:self.textField.text];
    self.textField.text = @"";
}
-(void)onLeftBtnTapped:(UIButton*) sender{
    [self.delegate onLeftBtnTapped:self.textField.text];
    self.textField.text = @"";
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self onRightBtnTapped:self.rightBtn];
    
    return false;
}
@end
